package com.gsmr.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gsmr.dao.TCHDao;
import com.gsmr.entity.APDU;
import com.gsmr.entity.Packet;
import com.gsmr.entity.TCH;
import com.gsmr.entity.TCH_Signaling;
import com.gsmr.service.TCHService;

/**
 * 用户Service实现类
 */
@Service("tchService")
public class TCHServiceImpl implements TCHService {

	@Resource
	private TCHDao tchDao;

	public List<TCH> find(Map<String, Object> map) {
		return tchDao.find(map);
	}

	public List<TCH> findASC(Map<String, Object> map) {
		return tchDao.findASC(map);
	}

	public List<TCH_Signaling> findSignaling(Map<String, Object> map) {
		return tchDao.findSignaling(map);
	}

	public List<TCH_Signaling> findSignalingASC(Map<String, Object> map) {
		return tchDao.findSignalingASC(map);
	}

	public Long getTotal(Map<String, Object> map) {
		return tchDao.getTotal(map);
	}

	public int update(Map<String, Object> map) {
		return tchDao.update(map);
	}

	public int add(Map<String, Object> map) {
		return tchDao.add(map);
	}

	public int delete(Map<String, Object> map) {
		return tchDao.delete(map);
	}

	public List<String> findTableNames(Map<String, Object> map) {
		return tchDao.findTableNames(map);
	}

	public List<TCH> findByIds(Map<String, Object> map) {
		return tchDao.findByIds(map);
	}

	public TCH findByPrimaryKey(Map<String, Object> map) {
		return tchDao.findByPrimaryKey(map);
	}

	public List<String> findDistricts(Map<String, Object> map) {
		return tchDao.findDistricts(map);
	}

	public List<TCH> findMessage_TYPEs(Map<String, Object> map) {
		return tchDao.findMessage_TYPEs(map);
	}

	public List<String> findFrontProcessorIds(Map<String, Object> map) {
		return tchDao.findFrontProcessorIds(map);
	}

	public List<TCH> findIMSIs(Map<String, Object> map) {
		return tchDao.findIMSIs(map);
	}

	public List<TCH> findMSISDNs(Map<String, Object> map) {
		return tchDao.findMSISDNs(map);
	}

	public List<TCH> findCTCS_IDs(Map<String, Object> map) {
		return tchDao.findCTCS_IDs(map);
	}

	public int createOrReplaceView(Map<String, Object> map) {
		return tchDao.createOrReplaceView(map);
	}

	@Override
	public List<String> findDecodeTable(Map<String, Object> map) {
		return tchDao.findDecodeTable(map);
	}

	@Override
	public List<Object> findMessageByThread(Map<String, Object> map) {
		return findType("threadId", map);
	}

	@Override
	public List<Object> findTCHFramedata(Map<String, Object> map) {
		return findType("timeStamp", map);
	}

	private List<Object> findType(String type, Map<String, Object> map) {
		Object tableType = map.get("messageType");
		if (tableType.equals("HDLC")) {
			return tchDao.findHDLC(map);
		} else if (tableType.equals("NPDU")) {
			return tchDao.findNPDU(map);
		} else if (tableType.equals("TPDU")) {
			return tchDao.findTPDU(map);
		} else if (tableType.equals("SaPDU")) {
			return tchDao.findSAPDU(map);
		} else if (tableType.equals("APDU")) {
			System.out.println("查找APDU的当日数据");
			Object apdu = tchDao.findAPDU(map);
			if (apdu == null || !(apdu instanceof List))
				return null;
			System.out.println(apdu);

			List<APDU> messages = (List<APDU>) apdu;
			if (messages.size() < 1)
				return null;

			APDU message = messages.get(0);
			if (message.getLablePacket() == null || !message.getLablePacket().contains(","))
				return Arrays.asList(message);

			System.out.println("LablePacket: " + message);
			String[] lablePacket = message.getLablePacket().split(",");
			if (lablePacket == null || lablePacket.length < 1)
				return Arrays.asList(message);

			System.out.println("lablePacket: " + Arrays.toString(lablePacket));
			for (int i = 0; i < lablePacket.length; i++) {
				if (lablePacket[i].length() < 5)
					continue;
				Map<String, String> m = new HashMap<>();
				Integer num = Integer.parseInt(lablePacket[i].substring(5), 2);
				m.put("table_name", "packet" + num + '_' + map.get(type));
				m.put("UUID", message.getUUID());
				Packet packet = tchDao.findPacket(m);
				System.out.println("packet: " + packet);
				message.getPacket().add(packet);
			}

			return Arrays.asList(message);
		}
		return null;
	}

	@Override
	public int findCTCS_ID(Map<String, Object> map) {
		return tchDao.findCTCS_ID(map);
		
	}
	
	public List<String> get_imsi(Map<String, Object> map) {
		return tchDao.get_imsi(map);
	}
	public List<String> get_msisdn(Map<String, Object> map) {
		return tchDao.get_msisdn(map);
	}
	public String get_ctcs_id(Map<String, Object> map) {
		return tchDao.get_ctcs_id(map);
	}
}
